
package com.shop.cloud.mall.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.core.constant.CacheConstants;
import com.shop.cloud.mall.common.entity.UserPoints;
import com.shop.cloud.mall.admin.mapper.UserPointsMapper;
import com.shop.cloud.mall.admin.service.UserPointsService;
import org.springframework.boot.autoconfigure.klock.annotation.Klock;
import org.springframework.boot.autoconfigure.klock.model.LockTimeoutStrategy;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

/**
 * 用户积分
 *
 * @author
 * @date 2023-11-23 10:28:55
 */
@Service
public class UserPointsServiceImpl extends ServiceImpl<UserPointsMapper, UserPoints> implements UserPointsService {


    @Override
    @CacheEvict(value = CacheConstants.APP_USER_INFO_CACHE, key = "#userId")
    @Klock(name = "updateUserPoints", keys = {"#userId"}, lockTimeoutStrategy = LockTimeoutStrategy.FAIL_FAST)
    public void updateUserPoints(String userId, Integer posts) {
        String sql = " points_current = points_current + " + posts;

        this.update(new UserPoints(), Wrappers.<UserPoints>lambdaUpdate()
                .eq(UserPoints::getUserId, userId)
                .setSql(sql));
    }
}
